// 发布订阅

class EventBus {
    constructor() {
        this.callbacks = {};
    }
    on(eventName, callback) {
        if (!this.callbacks[eventName]) {
            this.callbacks[eventName] = [];
        }
        this.callbacks[eventName].push(callback);
    }
    emit(eventName, ...args) {
        const events = this.callbacks[eventName];

        if(!events || !Array.isArray(events)) return;
        events.forEach(event => {
            event(...args);
        });
    }
    off(eventName, callback) {
        if(callback) {
            this.callbacks[eventName] =
            this.callbacks[eventName].filter(fn => fn !== callback);
        }else {
            this.callbacks[eventName] = null;
        }
    }
}

export default EventBus;